import requests
    #------------------------华为存储5510Fv
def cunchu_5510fv():
    # 定义请求 URL
    url = 'https://x.x.x.x:8088/deviceManager/rest/xxxxx/sessions'

    # 定义需要传递的参数
    payload = {
        'username': 'xxxxxxxxxxxxxx',
        'password': 'xxxxxxxxxxxxxxxxxx',
        'scope': '0'
    }

    # 发起 POST 请求
    requests.packages.urllib3.disable_warnings()
    response = requests.post(url, json=payload, verify=False)  # 关闭 SSL 验证，请谨慎使用

    # 检查响应状态码
    if response.status_code == 200:
        data = response.json()  # 将响应内容转换为 JSON 格式
        cookies = response.cookies  # 获取响应中的Cookie信息
        formatted_cookies = '; '.join([f"{cookie.name}={cookie.value}" for cookie in cookies])
        iBaseToken = data['data']['iBaseToken']  # 获取 iBaseToken 的值
        deviceid = data['data']['deviceid']  # 获取 deviceid 的值
        # ----------------查询设备状态信息------------------
        status_url = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/server/status'
        headers = {
            'Cookie': formatted_cookies,
            'iBaseToken': iBaseToken
        }
        status_response = requests.get(status_url, headers=headers, verify=False)
        status_mapping = {
            0: '正常',
            1: '异常',
            2: '上电中',
            3: '下电中',
            4: '安全保护状态',
            5: '升级中',
            6: '掉电',
            7: '离线'
        }

        if status_response.status_code == 200:
            status_data = status_response.json()
            if 'data' in status_data and 'status' in status_data['data']:
                device_status_code = status_data['data']['status']
                if device_status_code in status_mapping:
                    device_status_description = status_mapping[device_status_code]
                    print("设备状态:", device_status_description)
                else:
                    print("未知状态码:", device_status_code)
            else:
                print("未找到设备状态信息")
        else:
            print('设备状态信息查询失败，状态码：', status_response.status_code)
        # ------------------------查询当前告警信息——-----------------------
        alarm_url = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/alarm/currentalarm'
        alarm_response = requests.get(alarm_url, headers=headers, verify=False)
        alarm_details = ['无告警信息']  # 设置默认值为一个包含'无告警信息'的列表

        if alarm_response.status_code == 200:
            alarm_data = alarm_response.json()
            if 'data' in alarm_data and alarm_data['data']:
                alarms = alarm_data['data']
                # 获取所有告警的详细信息
                alarm_details = [alarm.get('detail', '无告警信息') for alarm in alarms]

                print("告警详情:", alarm_details)
            else:
                print("无告警信息")
        else:
            print('告警信息查询失败，状态码：', alarm_response.status_code)
        # ---------------------当前告警个数----------------------
        num_alarm = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/alarm/currentalarm/count'
        num_response = requests.get(num_alarm, headers=headers, verify=False)
        if num_response.status_code == 200:
            num_data = num_response.json()
            if 'data' in num_data and 'COUNT' in num_data['data']:
                num_alarms = num_data['data']['COUNT']
                print("当前告警个数:", num_alarms)
            else:
                print("未找到告警个数信息")
        else:
            print('告警个数信息查询失败，状态码：', num_response.status_code)
        # -----------------查询文件系统信息----------------------------
        # -----------------查询文件系统信息----------------------------
        print("-----------------------文件系统信息------------------------")
        system_url = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/filesystem'
        system_response = requests.get(system_url, headers=headers, verify=False)
        if system_response.status_code == 200:
            system_data = system_response.json()
            # NAME，CAPACITY，AVAILABLECAPCITY，HEALTHSTATUS，RUNNINGSTATUS，AVAILABLEANDALLOCCAPACITYRATIO
            # 检查是否有文件系统数据
            if 'data' in system_data:
                file_systems = system_data['data']
                file_system_data = []
                # 遍历文件系统列表
                for file_system in file_systems:
                    # 获取文件系统的名称，默认为'N/A'，以防找不到'NAME'
                    name = file_system.get('NAME', 'N/A')
                    # 获取系统的总容量
                    capacity = file_system.get('CAPACITY', 'N/A')
                    # 将总容量转化为TB
                    capacity = round(float(capacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                    if capacity < 1:  # 如果小于1T，单位转为GB
                        capacity = round(capacity * 1024, 3)
                        capacity = f'{capacity} GB'
                    else:
                        capacity = f'{capacity} TB'
                    # 获取系统的可用容量
                    availablecapcity = file_system.get('AVAILABLECAPCITY', 0)
                    # 将可用容量转化为GB
                    availablecapcity = round(float(availablecapcity) * 512 / 1024 / 1024 / 1024, 3)
                    if availablecapcity > 1000:  # 如果大于1T，单位转为TB
                        availablecapcity = round(availablecapcity / 1024, 3)
                        availablecapcity = f'{availablecapcity} TB'
                    else:
                        availablecapcity = f'{availablecapcity} GB'
                    # 获取系统的健康状态
                    healthstatus = file_system.get('HEALTHSTATUS', 0)
                    healthstatus = int(healthstatus)
                    if healthstatus == 1:
                        healthstatus = "健康"
                    else:
                        healthstatus = "异常"
                    # 获取系统的运行状态
                    runningstatus = file_system.get('RUNNINGSTATUS', 'N/A')
                    runningstatus = int(runningstatus)
                    if runningstatus == 27:
                        runningstatus = "在线"
                    else:
                        runningstatus = "异常"
                    availableandalloccappacitypatio = file_system.get('AVAILABLEANDALLOCCAPACITYRATIO', 'N/A')
                    availableandalloccappacitypatio = (float(availableandalloccappacitypatio))
                    if availableandalloccappacitypatio > 80:
                        availableandalloccappacitypatio = f'<span style="color:red;">{availableandalloccappacitypatio}%</span>'
                    else:
                        availableandalloccappacitypatio = f'<span style="color:green;">{availableandalloccappacitypatio}%</span>'
                    # 将文件系统数据添加到列表
                    file_system_data.append({
                        'name': name,
                        'capacity': capacity,
                        'availablecapcity': availablecapcity,
                        'healthstatus': healthstatus,
                        'runningstatus': runningstatus,
                        'availableandalloccappacitypatio': availableandalloccappacitypatio
                    })
            else:
                print("出错")
        # -------------------批量查询存储池信息---------------------
        print("-----------------存储池信息----------------------")
        storagepool_url = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/storagepool'
        storage_response = requests.get(storagepool_url, headers=headers, verify=False)
        if storage_response.status_code == 200:
            storage_data = storage_response.json()
            if 'data' in storage_data:
                storage_das = storage_data['data']
                # 假设存储池信息是一个列表，遍历每个存储池字典
                storage_system_data = []
                for storage_pool in storage_das:
                    # 从每个存储池字典中获取特定的键值对
                    name = storage_pool.get("NAME", "未知")
                    parent_name = storage_pool.get("PARENTNAME", "未知")
                    # # 根据条件判断健康状态和运行状态
                    health_status = storage_pool.get('HEALTHSTATUS', 0)
                    health_status = int(health_status)
                    if health_status == 1:
                        health_status = "健康"
                    else:
                        health_status = "异常"
                    # 获取系统的运行状态
                    running_status = storage_pool.get('RUNNINGSTATUS', 'N/A')
                    running_status = int(running_status)
                    if running_status == 27:
                        running_status = "在线"
                    else:
                        running_status = "异常"
                    user_total_capacity = storage_pool.get("USERTOTALCAPACITY", "未知")
                    user_consumed_capacity = storage_pool.get("USERCONSUMEDCAPACITY", "未知")
                    user_total_capacity_shiyong = round(float(user_total_capacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                    user_consumed_capacity_shiyong = round(float(user_consumed_capacity) * 512 / 1024 / 1024 / 1024 /1024, 3)
                    #用户使用率
                    shiyong_baifenbi = round(float(user_consumed_capacity_shiyong / user_total_capacity_shiyong) * 100,3)
                    # 根据使用率设置颜色,\033[91m 用于设置文本颜色为红色，\033[0m 用于重置颜色
                    if shiyong_baifenbi > 80:
                        shiyong_baifenbi = f'<span style="color:red;">{shiyong_baifenbi}%</span>'
                    else:
                        shiyong_baifenbi = f'<span style="color:green;">{shiyong_baifenbi}%</span>'

                    user_total_capacity = round(float(user_total_capacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                    if user_total_capacity < 1:  # 如果小于1T，单位转为GB
                        user_total_capacity = round(user_total_capacity * 1024, 3)
                        user_total_capacity = f'{user_total_capacity} GB'
                    else:
                        user_total_capacity = f'{user_total_capacity} TB'
                    # 用户消耗的容量
                    user_consumed_capacity = round(float(user_consumed_capacity) * 512 / 1024 / 1024 / 1024, 3)
                    if user_consumed_capacity > 1000:  # 如果大于1T，单位转为TB
                        user_consumed_capacity = round(user_consumed_capacity / 1024, 3)
                        user_consumed_capacity = f'{user_consumed_capacity} TB'
                    else:
                        user_consumed_capacity = f'{user_consumed_capacity} GB'
                    # 空闲的容量
                    user_free_capacity = storage_pool.get("USERFREECAPACITY", "未知")

                    # 将文件系统数据添加到列表
                    storage_system_data.append({
                        'name': name,
                        'shiyong_baifenbi': shiyong_baifenbi,
                        'health_status': health_status,
                        'running_status': running_status,
                        'user_total_capacity': user_total_capacity,
                        'user_consumed_capacity': user_consumed_capacity
                    })
        else:
            print('存储池信息查询失败，状态码：', storage_response.status_code)
        # ---------------------批量查询lun---------------------
        print("-----------------批量查询lun信息----------------------")
        lun_url = f'https://10.106.1.131:8088/deviceManager/rest/{deviceid}/lun'
        lun_response = requests.get(lun_url, headers=headers, verify=False)
        # 处理响应
        if lun_response.status_code == 200:
            fv_lun_data = lun_response.json()['data']
            lun_system_data = []
            # 遍历前两个字典，提取并打印指定项的键值对
            for i in range(min(2, len(fv_lun_data))):
                lun_info = fv_lun_data[i]
                name = lun_info.get("NAME", "未知")
                parentname = lun_info.get("PARENTNAME", "未知")
                # # 根据条件判断健康状态和运行状态
                health_status = storage_pool.get('HEALTHSTATUS', 0)
                health_status = int(health_status)
                if health_status == 1:
                    health_status = "健康"
                else:
                    health_status = "异常"
                # 获取系统的运行状态
                running_status = storage_pool.get('RUNNINGSTATUS', 'N/A')
                running_status = int(running_status)
                if running_status == 27:
                    running_status = "在线"
                else:
                    running_status = "异常"
                total_saved_capacity = lun_info.get("CAPACITY", "未知")
                alloccapacity = lun_info.get("ALLOCCAPACITY", "未知")
                #计算lun使用率
                total_saved_capacity_shiyong = round(float(total_saved_capacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                alloccapacity_shiyong = round(float(alloccapacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                lun_shiyong_baifenbi = round(float(alloccapacity_shiyong / total_saved_capacity_shiyong) * 100,3)
                if lun_shiyong_baifenbi > 80:
                    lun_shiyong_baifenbi = f'<span style="color:red;">{lun_shiyong_baifenbi}%</span>'
                else:
                    lun_shiyong_baifenbi = f'<span style="color:green;">{lun_shiyong_baifenbi}%</span>'


                total_saved_capacity = round(float(total_saved_capacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                if total_saved_capacity < 1:  # 如果小于1T，单位转为GB
                    total_saved_capacity = round(total_saved_capacity * 1024, 3)
                    total_saved_capacity = f'{total_saved_capacity} GB'
                else:
                    total_saved_capacity = f'{total_saved_capacity} TB'

                alloccapacity = round(float(alloccapacity) * 512 / 1024 / 1024 / 1024 / 1024, 3)
                if alloccapacity < 1:  # 如果小于1T，单位转为GB
                    alloccapacity = round(alloccapacity * 1024, 3)
                    alloccapacity = f'{alloccapacity} GB'
                else:
                    alloccapacity = f'{alloccapacity} TB'

                # 将文件系统数据添加到列表
                lun_system_data.append({
                    'name': name,
                    'total_saved_capacity': total_saved_capacity,
                    'health_status': health_status,
                    'running_status': running_status,
                    'lun_shiyong_baifenbi': lun_shiyong_baifenbi,
                    'alloccapacity': alloccapacity
                })
        else:
            print('LUN 信息查询失败，状态码：', lun_response.status_code)
    return {
        'device_status_description': device_status_description,
        'num_alarms': num_alarms,
        'alarm_details': alarm_details,
        'file_systems': file_system_data,
        'storage_pools': storage_system_data,
        'fv_lun_data': lun_system_data,
    }
cunchu_5510fv()